#  版本一：url+字典拼合法查找

# 用到了很古老的一个东西————PhantomJS 因为页面加载时电费是JS动态加载，这个办法是动态加载页面后获取了相关的数据
# selenium版本3.8   
# 增加了一项功能————模糊音处理问题，解决了因打错字而导致的查询失败

from selenium import webdriver
import os

url = 'http://www.houqinbao.com/hydropower/index.php?rebind=1&m=PayWeChat&c=Index&a=bingding&token=&openid=oUiRowXbORPJT9eWljFST1-UzoWg&schoolcode=13579&payopenid='
area_dic = {
            "杏一":"13579_173_288_948", "杏二":"13579_173_288_949", "杏三":"13579_173_288_950", 
            "竹一":"13579_173_289_951", "竹二":"13579_173_289_952", "竹三":"13579_173_289_953", "竹四":"13579_173_289_954", 
            "松一":"13579_173_290_955", "松二":"13579_173_290_956", "松三":"13579_173_290_957", "松四":"13579_173_290_958", 
            "桃一":"13579_173_291_959", "桃二":"13579_173_291_960", "桃三":"13579_173_291_961", "桃四":"13579_173_291_962", "桃五":"13579_173_291_963",
            "研一":"13579_173_293_967", "研二":"13579_173_293_968", "研三":"13579_173_293_969"
            }

fuzzy_pro = {
# 杏苑
    "X一":"杏一","杏苑一号楼":"杏一","杏一楼":"杏一","x一":"杏一","杏院一号楼":"杏一","杏院一楼":"杏一","杏1楼":"杏一","杏1":"杏一","X1":"杏一","x1":"杏一",
    "X二":"杏二","杏苑二号楼":"杏二","杏二楼":"杏二","x二":"杏二","杏院二号楼":"杏二","杏院二楼":"杏二","杏2楼":"杏一","杏2":"杏一","X2":"杏二","x2":"杏二",
    "X三":"杏三","杏苑三号楼":"杏三","杏三楼":"杏三","x三":"杏三","杏院三号楼":"杏三","杏院三楼":"杏三","杏3楼":"杏三","杏3":"杏三","X3":"杏三","x3":"杏三",
# 竹苑
    "Z一":"竹一","竹苑一号楼":"竹一","竹一楼":"竹一","z一":"竹一","竹院一号楼":"竹一","竹院一楼":"竹一","竹1楼":"竹一","竹1":"竹一","z1":"竹一","Z1":"竹一",
    "Z二":"竹二","竹苑二号楼":"竹二","竹二楼":"竹二","z二":"竹二","竹院二号楼":"竹二","竹院二楼":"竹二","竹2楼":"竹二","竹2":"竹二","z2":"竹二","Z2":"竹二",
    "Z三":"竹三","竹苑三号楼":"竹三","竹三楼":"竹三","z三":"竹三","竹院三号楼":"竹三","竹院三楼":"竹三","竹3楼":"竹三","竹3":"竹三","z3":"竹三","Z3":"竹三",
    "Z四":"竹四","竹苑四号楼":"竹四","竹四楼":"竹四","z四":"竹四","竹院四号楼":"竹四","竹院四楼":"竹四","竹4楼":"竹四","竹4":"竹四","z4":"竹四","Z4":"竹四",
# 松苑
    "S一":"松一","松苑一号楼":"松一","松一楼":"松一","s一":"松一","松院一号楼":"松一","松院一楼":"松一","松1楼":"松一","松1":"松一","S1":"松一","s1":"松一",
    "S二":"松二","松苑二号楼":"松二","松二楼":"松二","s二":"松二","松院二号楼":"松二","松院二楼":"松二","松2楼":"松二","松2":"松二","S2":"松二","s2":"松二",
    "S三":"松三","松苑三号楼":"松三","松三楼":"松三","s三":"松三","松院三号楼":"松三","松院三楼":"松三","松3楼":"松三","松3":"松三","S3":"松三","s3":"松三",
    "S四":"松四","松苑四号楼":"松四","松四楼":"松四","s四":"松四","松院四号楼":"松四","松院四楼":"松四","松4楼":"松四","松4":"松四","S4":"松四","s4":"松四",
# 桃苑
    "T一":"桃一","松苑一号楼":"桃一","桃一楼":"桃一","t一":"桃一","松院一号楼":"桃一","桃院一楼":"桃一","桃1楼":"桃一","桃1":"桃一","T1":"桃一","t1":"桃一",
    "T二":"桃二","桃苑二号楼":"桃二","桃二楼":"桃二","t二":"桃二","桃院二号楼":"桃二","桃院二楼":"桃二","桃2楼":"桃二","桃2":"桃二","T2":"桃二","t2":"桃二",
    "T三":"桃三","桃苑三号楼":"桃三","桃三楼":"桃三","t三":"桃三","桃院三号楼":"桃三","桃院三楼":"桃三","桃3楼":"桃三","桃3":"桃三","T3":"桃三","t3":"桃三",
    "T四":"桃四","桃苑四号楼":"桃四","桃四楼":"桃四","t四":"桃四","桃院四号楼":"桃四","桃院四楼":"桃四","桃4楼":"桃四","桃4":"桃四","T4":"桃四","t4":"桃四",
    "T五":"桃五","桃苑五号楼":"桃五","桃五楼":"桃五","t五":"桃五","桃院五号楼":"桃五","桃院五楼":"桃五","桃5楼":"桃五","桃5":"桃五","T5":"桃五","t5":"桃五",
# 研究生苑
    "Y一":"研一","研究生一号楼":"研一","研一楼":"研一","y一":"研一","研究生一号楼":"研一","研究生一楼":"研一","研1楼":"研一","研1":"研一","Y1":"研一","y1":"研一",
    "Y二":"研二","研究生二号楼":"研二","研二楼":"研二","y二":"研二","研究生二号楼":"研二","研究生二楼":"研二","研2楼":"研一","研2":"研一","Y2":"研二","y2":"研二",
    "Y三":"研三","研究生三号楼":"研三","研三楼":"研三","y三":"研三","研究生三号楼":"研三","研究生三楼":"研三","研3楼":"研三","研3":"研三","Y3":"研三","y3":"研三",
}

def url_add( areaid , roomid ):
    try:
        if areaid not in area_dic:
            areaid = fuzzy_pro[areaid]
        id_list = area_dic[areaid].split('_')
    except:
        print("输入房间不存在，请求错误!")
        return False
    return "http://www.houqinbao.com/hydropower/index.php?m=PayWeChat&c=Index&a=before&payopenid=&openid=oUiRowXbORPJT9eWljFST1-UzoWg&schoolcode={}&campusid={}&areaid={}&flatid={}&roomname={}".format(id_list[0] , id_list[0]+"_"+id_list[1] , id_list[0]+"_"+id_list[1]+"_"+id_list[2] , id_list[0]+"_"+id_list[1]+"_"+id_list[2]+"_"+id_list[3] , roomid)  
# print(url_add("桃四楼","B3102"))

def ele_ask(nameid , roomid):  # 查询
    try:
        try:
            driver = webdriver.PhantomJS(executable_path=r'phantomjs.exe')
        except:
            print("缺少关键文件PhantomJS")
        driver.get(url_add(nameid , roomid.upper()))
        ele_pay = driver.find_element_by_class_name("dushu").text
        print(ele_pay)
        driver.quit()
    except:
        print("发生异常,不是你网断了就是垃圾端口又崩了...")

def input_id(text):  #获取信息
    return input(text)

def try_move():   #删除已有的日志
    try:
        os.remove("ghostdriver.log")
    except:
        pass

if __name__ == "__main__":
    try_move()
    name = input_id("寝室楼:")
    room = input_id("房间号:")
    ele_ask(name , room) #....测试数据....
    input("按回车结束...")
    try_move()

# 其他的版本想起来就搞一搞，搞完弄到"Gay Hub"上去